package loader;

import java.sql.SQLException;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import loader.persister.DealPersister;
import loader.persister.Persister;

public class TimerLoader {

	private String[] cities;
	private Persister persister;
	private Timer timer;

	public TimerLoader(String[] cities) {
		this.cities = cities;
	}

	public void load(int delay) {

		final Loader loader = new Loader();
		persister = new DealPersister();
		// persister.clear();
		loader.setPersister(persister);

		timer = new Timer(true);
		timer.schedule(new TimerTask() {
			public void run() {
				System.out.println(new Date().getTime());
				for (String city : cities)
					loader.load(city.trim());
				System.out.println(new Date().getTime());
			}
		}, 0, delay*60*1000);// milliseconds
	}

	public void close() {
		try {
			this.persister.close();
			this.timer.cancel();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
